package com.yunkaweilai.android.view.dialog.operation;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.github.lazylibrary.util.StringUtils;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.utils.InputMethodUtils;
import com.yunkaweilai.android.utils.MoneyTextWatcher;
import com.yunkaweilai.android.view.MyToast;

import java.util.Timer;
import java.util.TimerTask;


public class DialogPayMoney extends Dialog implements View.OnClickListener{

    private Context mContext;
    private OnCloseListener listener;
    private String title;
    private TextView idTvContent;
    private EditText idEdtTypeName;
    private TextView idTvRight;
    private TextView idTvClose;
    private TextView idTvMaxType;
    private String content;


    public DialogPayMoney(Context context, int themeResId, OnCloseListener listener,String content,String title) {
        super(context, themeResId);
        this.mContext = context;
        this.title = title;
        this.listener = listener;
        this.content = content;
    }


    public DialogPayMoney setTitle(String title){
        this.title = title;
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_receivables_money);
        initView();
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run(){
                InputMethodManager inputManager = (InputMethodManager) idEdtTypeName.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(idEdtTypeName, 0);
            }
        },800);
    }

    private void initView(){
        idEdtTypeName = ((EditText) findViewById(R.id.id_edt_type_name));
        idEdtTypeName.addTextChangedListener(new MoneyTextWatcher(idEdtTypeName));
        idTvRight = ((TextView) findViewById(R.id.id_tv_right));
        idTvClose = ((TextView) findViewById(R.id.id_tv_close));
        idTvMaxType = ((TextView) findViewById(R.id.id_tv_max_type));
        idTvMaxType .setText(title);
        idTvRight.setOnClickListener(this);
        idTvClose.setOnClickListener(this);
        idEdtTypeName.setText(content + "");
        idEdtTypeName.setSelection(idEdtTypeName.length());
//        idEdtTypeName.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                String temp = s.toString();
//                int posDot = temp.indexOf(".");
//                if (posDot <= 0) return;
//                if (temp.length() - posDot - 1 > 2) {
//                    s.delete(posDot + 3, posDot + 4);
//                }
//            }
//        });

        idEdtTypeName.setFocusable(true);
        idEdtTypeName.setFocusableInTouchMode(true);
        idEdtTypeName.requestFocus();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.id_tv_close:
                if(listener != null){
                    listener.onClick(this, false,"");
                }
                this.dismiss();
                break;
            case R.id.id_tv_right:
                InputMethodUtils.closeSoftKeyboard((BaseActivity)mContext);
                if(listener != null){
                    if (!StringUtils.isEmpty(idEdtTypeName.getText().toString())){
                        listener.onClick(this, true,idEdtTypeName.getText().toString());
                        InputMethodUtils.closeSoftKeyboard((BaseActivity)mContext);
                    }else{
                        MyToast.showShortToast(mContext,"还未输入金额");
                    }
                }
                break;
        }
    }

    public interface OnCloseListener{
        void onClick(Dialog dialog, boolean confirm, String peyMoney);
    }
}
